package com.fy.tt.executor.node.sentence.simpleStmt;

import com.fy.toolhelper.tool.ActionTool;
import com.fy.tt.executor.environment.PolicyWorker;

import com.fy.tt.executor.node.word.noun.Band;
import com.fy.tt.executor.node.word.noun.Tool;
import com.fy.tt.executor.node.word.noun.Unit;
import com.fy.tt.executor.node.word.noun.User;
import com.fy.tt.parser.V3Parser;
import com.fy.tt.permission.PolicyCheckModel;


import java.lang.reflect.InvocationTargetException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

public class RunToolStmt extends IntenseStmt{
    //A在B帮区执行C工具D部件，参数为XX
    private User user;
    private Band band;
    private Tool tool;
    private Unit unit;
    private HashMap<String,Object> param;
    private V3Parser.RunToolStmtContext ctx;

    public RunToolStmt(V3Parser.RunToolStmtContext ctx, Map<String, Object> scriptEnvironment) {
        this.ctx=ctx;
        user = new User(ctx.Identifier().get(0).getText(), scriptEnvironment);
        band = new Band(ctx.place().Identifier().getText(), scriptEnvironment);
        tool = new Tool(ctx.Identifier().get(1).getText(), scriptEnvironment);
        if(ctx.Unit()!=null){
            unit = new Unit(ctx.Identifier().get(2).getText(), scriptEnvironment);
        }

    }

//

    @Override
    public boolean run(String url) {
        // 工具调用的url
        try {
            if(!checkRunToolPermission()){
                System.out.println("没有权限执行工具");
                return false;
            }
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        System.out.println("拥有权限执行工具");
        System.out.println("调用工具："+tool.text);
        return true;
    }


    public boolean checkRunToolPermission() throws InvocationTargetException, IllegalAccessException {
        PolicyWorker policyWorker= ActionTool.getBean(PolicyWorker.class);
        boolean canAct;
        GregorianCalendar calendar = new GregorianCalendar();
        int hour = calendar.get(Calendar.HOUR_OF_DAY) ;
        Map<String,Object> env=new HashMap<>();
        Map<String,Object> time=new HashMap<>();
        time.put("hour",hour);
        env.put("time",time);

        String Object =tool.id+".*";
        String operration="can.act";
        Integer canActTool,canActUnit;
        PolicyCheckModel policyCheckModel=new PolicyCheckModel(user,Object,operration,env,band.getObjID());
        canActTool=policyWorker.checkPolicy(policyCheckModel);
        if(canActTool==-1){//被禁止访问工具
            canAct=false;
            return canAct;
        }
        if(unit.text!=null){//判断能否访问部件
            Object=tool.id+"."+unit.text.trim();
            PolicyCheckModel policyCheckModelForUnit=new PolicyCheckModel(user,Object,operration,env,band.getObjID());
            canActUnit=policyWorker.checkPolicy(policyCheckModelForUnit);
            if(canActUnit==-1){//被禁止访问部件
                canAct=false;
                return canAct;
            }
            if(canActUnit==1||canActTool==1){//工具和部件层次都没有被允许
                canAct=true;
                return canAct;
            }
        }

        canAct=false;
        return canAct;
    }


}
